<?php
/*
 *      Redistribution and use in source and binary forms, with or without
 *      modification, are permitted provided that the following conditions are
 *      met:
 *      
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with the
 *        distribution.
 *      * Neither the name of the  nor the names of its
 *        contributors may be used to endorse or promote products derived from
 *        this software without specific prior written permission.
 *      
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

abstract class ti_interface
{	
	function table()
	{
		global $ti;
		$this->swap_block('center','table');
	}
		
	function view()
	{
		global $ti;
		$this->swap_block('center','view');
	}
	
	function init($path)
	{
		global $ti;
		$this->class   = get_class($this);
		$this->path    = dirname($path);
		$this->data    =& $ti->data;
		$this->interfaces   = array();
		
		if(file_exists($this->path.'/libs/queries.php'))
		{
			include($this->path.'/libs/queries.php');
		}
		if(file_exists($this->path.'/libs/phrases.php'))
		{
			include($this->path.'/libs/phrases.php');
		}
		if(file_exists($this->path.'/libs/phrases.'.$ti->config['vars.language'].'.php'))
		{
			include($this->path.'/libs/phrases.'.$ti->config['vars.language'].'.php');
		}
		if(file_exists($this->path.'/libs/phrases.'.$ti->config['vars.language'].'_'.$ti->config['vars.language.variant'].'.php'))
		{
			include($this->path.'/libs/phrases.'.$ti->config['vars.language'].'_'.$ti->config['vars.language.variant'].'.php');
		}
	}
	
	function ints()
	{
		$args = func_get_args();
		foreach($args as $arg)
		{
			$this->data[$arg] = intval($this->data[$arg]);
		}
	}
	
	function floats()
	{
		$args = func_get_args();
		foreach($args as $arg)
		{
			$this->data[$arg] = floatval($this->data[$arg]);
		}
	}
	
	function dates()
	{
		$args = func_get_args();
		foreach($args as $arg)
		{
			$this->data[$arg] = intval($this->data[$arg]);
		}
	}
	
	function query($query,$data=null)
	{
		global $ti;
		return $ti->query($this->class,$query,$data);
	}
	
	function query_keyhash($key,$query,$data=null)
	{
		global $ti;
		return $ti->query_keyhash($key,$this->class,$query,$data);
	}
	
	function query_row($query,$data=null)
	{
		global $ti;
		return $ti->query_row($this->class,$query,$data);
	}
	
	function query_value($query,$col,$data=null)
	{
		global $ti;
		return $ti->query_value($this->class,$query,$col,$data);
	}
	
	function query_block($block,$query,$data=null)
	{
		global $ti;
		$html = '';
		$query = $ti->query($this->class,$query,$data);
		while($query->next())
		{
			$html .= $this->block($block,$query->data);
		}
		return $html;
	}
	
	function phrase($phrase,$data=null)
	{
		global $ti;
		return $ti->phrase($this->class,$phrase,$data);	
	}
	
	function swap_phrase($position,$phrase,$data=null)
	{
		global $ti;
		$ti->swap[$position] .= $this->phrase($phrase,$data);
	}
	
	function load_block($block,$data=null)
	{
		global $ti;
		#echo($block.' data: '.print_r($data,true)."\n<br />");
		if(file_exists($this->path.'/blocks/'.$block.'.html'))
		{
			$this->blocks[$block] = file_get_contents($this->path.'/blocks/'.$block.'.html');
			return 'html';
		}
		if(file_exists($this->path.'/blocks/'.$block.'.php'))
		{
			include($this->path.'/blocks/'.$block.'.php');
			if(is_string($this->blocks[$block]))
			{
				$class = $this->blocks[$block];
				$this->blocks[$block] = new $class($data);
				$this->blocks[$block]->render($data);
				return 'class';
			}
			else if(is_null($this->blocks[$block]))
			{
				$new_class = $this->class.'_'.$block;
				$this->blocks[$block] = new $new_class();
				$this->blocks[$block]->render($data);
				return 'class';
			}
			else
			{
				$this->blocks[$block]->render($data);
				return 'php';
			}
		}
	}
	
	function block($block,$data=null)
	{
		global $ti;
		$type = $this->load_block($block,$data);
		if($type == 'html')
		{
			$html = $this->blocks[$block];
		}
		else
		{
			$ti->swap['js'] .= $this->blocks[$block]->js;
			$html = $this->blocks[$block]->html;
		}
		if(is_null($data))
		{
			return $html;
		}
		else
		{
			return ti_util::swap($html,$data);
		}
	}
	
	function swap_block($position,$html,$data=null)
	{
		global $ti;
		$ti->swap[$position] .= $this->block($html,$data);
	}
	
	function get_block_data()
	{
		global $ti;
		$block = $ti->data['block'];		
		$block_file = substr($block,strlen($this->class)+1,strlen($block));
		$ti->log('trying to load block: '.$block.'/'.$block_file);
		$this->load_block($block_file);
		$block = new $block();
		$block->get_data();
		$ti->ajax();
	}
}

?>